package com.example.wallpaperapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.wallpaperapp.R;
import com.example.wallpaperapp.bean.HomeImageRecyclerViewBean;
import com.example.wallpaperapp.ui.activity.Activity_setting;
import com.example.wallpaperapp.ui.activity.CompilationsActivity;
import com.example.wallpaperapp.widget.RoundImageView2;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import butterknife.BindView;

/**
 * @Package: com.example.wallpaperapp.adapter
 * @ClassName: HomeComAdapter
 * @Description: java类作用描述
 * @Author: zrmo
 * @CreateDate: 2020/10/30 20:55
 */

public class HomeComAdapter extends RecyclerView.Adapter<HomeComAdapter.ViewHolder> {
    private List<HomeImageRecyclerViewBean> lists;
    private Context mContext ;
    private OnItemClickListener mOnItemClickListener;
    public Drawable img[];
    private int a;
    private Bitmap bitmap;

    public HomeComAdapter(Context context,List<HomeImageRecyclerViewBean> list) {
        mContext = context;
        lists = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_com,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);


        return viewHolder;

    }
    @NonNull
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position)  {

        for (int i = 0; i < lists.size(); i++) {
            holder.itemView.getId();
        }
        HomeImageRecyclerViewBean bean = (HomeImageRecyclerViewBean) lists.get(position);

        for (int i = 0; i < 6; i++) {
            holder.mIv[i].setImageBitmap(returnBitMap(bean.image_url_list[i]));
        }

        //设置点击事件
        for (int i = 0; i < 6; i++) {

            if (mOnItemClickListener != null && !holder.mIv[i].hasOnClickListeners()) {
                holder.mIv[i].setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        //回调方法,,将view控件和索引传回,索引必须是布局在父布局中的索引,否则索引错乱最终报错
                        mOnItemClickListener.onItemClicked(holder.getLayoutPosition(), v);
                        a=holder.getLayoutPosition();
                    }
                });
            }
            Glide.with(mContext).load(bean.image_url_list[i]).into(holder.mIv[i]);

        }
    }
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


    public interface OnItemClickListener{

        void onItemClicked( int position,View view);

    }
    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {



        RoundImageView2[] mIv = new RoundImageView2[6];

        public ViewHolder(View itemView) {
            super(itemView);
            mIv[0] = itemView.findViewById(R.id.com_image_1);
            mIv[1] = itemView.findViewById(R.id.com_image_2);
            mIv[2] = itemView.findViewById(R.id.com_image_3);
            mIv[3] = itemView.findViewById(R.id.com_image_4);
            mIv[4] = itemView.findViewById(R.id.com_image_5);
            mIv[5] = itemView.findViewById(R.id.com_image_6);

        }
    }
    /**
     * 将URL转化成bitmap形式
     *
     * @param url
     * @return bitmap type
     */
    public Bitmap returnBitMap(final String url){

        new Thread(new Runnable() {
            @Override
            public void run() {
                URL imageurl = null;


                try {
                    imageurl = new URL(url);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                try {
                    HttpURLConnection conn = (HttpURLConnection)imageurl.openConnection();
                    conn.setDoInput(true);
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    bitmap = BitmapFactory.decodeStream(is);
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        return bitmap;
    }
}
